package com.jack.bookshop.servlet;
import com.jack.bookshop.dao.BookDao;
import com.jack.bookshop.vo.PageVo;
import com.jack.bookshop.vo.BookVo;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "BookSearchServlet",value = "/BookSearchServlet")
public class BookSearchServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 读取参数
        String str = request.getParameter("str");

        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");

        // 把字符串转换成数字
        int curPage = 1; // 注意，page是JSP的关键词，不能用作变量名
        int size = 4;
        if (pageStr != null) {
            curPage = Integer.parseInt(pageStr);
        }
        if (sizeStr != null) {
            size = Integer.parseInt(sizeStr);
        }

        // 调用dao层，完成搜索
//        PageVo<BookVo> pageVo = new BookDao().getListBySearch(str, curPage, size);
        PageVo<BookVo> pageVo = new BookDao().getListByFullText(str, curPage, size);

        // 把搜索关键词和搜索结果，存入request中
        request.setAttribute("str", str);
        request.setAttribute("pageVo", pageVo);
        //System.out.println(pageVo);
        // 转发到到 search.jsp 显示搜索结果
        request.getRequestDispatcher("search.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
